package org.fessingapore.library.services;

import org.fessingapore.library.daos.impl.GenericDaoImpl;
import org.fessingapore.library.daos.GenericDao;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GenericService<T> implements GenericServiceInterface<T> {

    private Class<T> persistentClass;
    private GenericDao daoClass;

    public GenericService(){
    	construct();
    }
    
    public void construct() {

        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        try {
        	String temp=persistentClass.getName();
            temp=temp.replace("models","daos.impl");
            Class cls = Class.forName(temp + "DaoImpl");
            
            Object obj = cls.newInstance();
             
            daoClass = (GenericDaoImpl) obj;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);

        } catch (InstantiationException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    public List<T> findAll() {
        List<T> tList = null;
        try {
            tList = daoClass.findAll();
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tList;
    }


    public T create(T t) {
        try {
            t = (T) daoClass.create(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        } catch(Exception ex){
               Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return t;
    }

    
    public boolean update(T t) {
        boolean success = false;
        try {
            success = daoClass.update(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return success;
    }


    public boolean delete(T t) {
        boolean success = false;
        try {
            success = daoClass.delete(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return success;
    }

    public T getById(int id) {
        T t = null;
        try {
            t = (T) daoClass.getById(id);
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return t;
    }

  
    public List<T> search(T t) {
        List<T> tList = null;
        try {
            tList = daoClass.search(t);
        } catch (SQLException ex) {
            Logger.getLogger(GenericService.class.getName()).log(Level.SEVERE, null, ex);
        }
         return tList;
    }
}
